package Control;
import java.util.*;


public class Character {
	private String name;
	private String shortName;
	private Episode firstAppearence;
	private ArrayList<CharacterAffine> affineCharacters;
	private ArrayList<CatchPhrase> catchphrases;
	private ArrayList<CommonWord> commonWords;
	private String imagePath;
	public static CharacterComparator comparator = new CharacterComparator();
	
	
	public Character() {
		super();
		affineCharacters = new ArrayList<CharacterAffine>();
		catchphrases = new ArrayList<CatchPhrase>();
		commonWords = new ArrayList<CommonWord>();
	}

	public Character(String name, Episode firstAppearence,
			ArrayList<CharacterAffine> affineCharacters,
			ArrayList<CatchPhrase> catchphrases,
			ArrayList<CommonWord> commonWords) {
		super();
		this.setName(name);
		this.firstAppearence = firstAppearence;
		this.affineCharacters = affineCharacters;
		this.catchphrases = catchphrases;
		this.commonWords = commonWords;
	}
	
	

	public Character(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	
	
	
	
	public void setName(String name) {
		if(name.equalsIgnoreCase("fry")){
			this.name = "Philip J. Fry";
			setupCatchPhrases(this.name);
			return;
		}
		if(name.equalsIgnoreCase("leela")){
			this.name = "Turanga Leela";
			setupCatchPhrases(this.name);
			return;
		}
		if(name.equalsIgnoreCase("bender")){
			this.name = "Bender Bending Rodriguez";
			setupCatchPhrases(this.name);
			return;
		}
		if(name.equalsIgnoreCase("zoidberg")){
			this.name = "Dr. John A. Zoidberg";
			setupCatchPhrases(this.name);
			return;
		}
		if(name.equalsIgnoreCase("zapp") || name.toLowerCase().contains("zapp") ||
			name.toLowerCase().contains("brannigan")){
			this.name = "Zapp Brannigan";
			return;
		}
		if(name.equalsIgnoreCase("amy")){
			this.name = "Amy Wong";
			return;
		}
		if(name.equalsIgnoreCase("kif")){
			this.name = "Kif Kroker";
			return;
		}
		if(name.equalsIgnoreCase("hermes")){
			this.name = "Hermes Conrad";
			setupCatchPhrases(this.name);
			return;
		}
		if(name.equalsIgnoreCase("labarbara")){
			this.name = "Labarbara Conrad";
			return;
		}
		if(name.equalsIgnoreCase("dwight")){
			this.name = "Dwight Conrad";
			return;
		}
		if(name.equalsIgnoreCase("farnsworth") || name.toLowerCase().contains("farnsworth")){
			this.name = "Prof Hubert J. Farnsworth";
			setupCatchPhrases(this.name);
			return;
	}
		if(name.equalsIgnoreCase("cubert")){
			this.name = "Cubert Farnsworth";
			return;
		}
		if(name.equalsIgnoreCase("wernstrom") || name.equalsIgnoreCase("wermstrom")){
			this.name = "Ogden Wernstrom";
			return;
		}
		this.name = name;
		
	}
	
	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
		this.setName(shortName);
	}

	public Episode getFirstAppearence() {
		return firstAppearence;
	}

	public void setFirstAppearence(Episode firstAppearence) {
		this.firstAppearence = firstAppearence;
	}

	public ArrayList<CharacterAffine> getCompleteAffineCharacters() {
		return affineCharacters;
	}
	
	public ArrayList<CharacterAffine> getAffineCharacters(){
		ArrayList<CharacterAffine> results = new ArrayList<CharacterAffine>();
		for(int i = 0; i < this.affineCharacters.size() && i < 10; i++)
			results.add(this.affineCharacters.get(i));
		return results;
	}

	public void setAffineCharacters(ArrayList<CharacterAffine> affineCharacters) {
		this.affineCharacters = affineCharacters;
	}

	public ArrayList<CatchPhrase> getCatchphrases() {
		return catchphrases;
	}
	
	public void addCommonWord(CommonWord cw){
		this.commonWords.add(cw);
	}
	
	public ArrayList<CommonWord> getCompleteCommonWords(){
		return this.commonWords;
	}

	
	public ArrayList<CommonWord> getCommonWords() {
		ArrayList<CommonWord> results = new ArrayList<CommonWord>();
		for(int i = 0; i < this.commonWords.size() && i < 10; i++){
			results.add(this.commonWords.get(i));
		}
		return results;
	}

	public void setCommonWords(ArrayList<CommonWord> commonWords) {
		this.commonWords = commonWords;
	}
	
	private void setupCatchPhrases(String name){
		if(name.equalsIgnoreCase("Philip J. Fry")){
			CatchPhrase c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Help! Police!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("He's dead");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("OW! My (random body part)!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Bender? Is that you?");
			this.catchphrases.add(c);
			return;
		}
		if(name.equalsIgnoreCase("Dr. John A. Zoidberg")){
			CatchPhrase c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Woop-woop-woop-woop!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Hello");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("What an honour!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Excellent, excellent!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("I love that guy!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Hooray");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("I'll take eight");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Dibs on...");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Awwww...");
			this.catchphrases.add(c);
			return;
		}
		if(name.equalsIgnoreCase("Bender Bending Rodriguez")){
			CatchPhrase c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Bite my shiny metal ass!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Me, Bender!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("We're boned");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Bender is great");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Neat!");
			this.catchphrases.add(c);
			return;
		}
		if(name.equalsIgnoreCase("Prof Hubert J. Farnsworth")){
			CatchPhrase c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Good news, everyone!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("I am already in my pajamas");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Sweet Zombie Jesus!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Huh?");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Wernstrom!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Fuff!");
			this.catchphrases.add(c);
			return;
		}
		if(name.equalsIgnoreCase("Turanga Leela")){
			CatchPhrase c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Hi-yah!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Oh, Lord");
			this.catchphrases.add(c);
			return;
		}
		if(name.equalsIgnoreCase("Hermes Conrad")){
			CatchPhrase c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("Sweet (animal/thing) of (place)!");
			this.catchphrases.add(c);
			c = new CatchPhrase();
			c.setCharacter(this);
			c.setPhrase("My manwitch!");
			this.catchphrases.add(c);
			return;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Character other = (Character) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equalsIgnoreCase(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Character name=" + name + ", shortName=" + shortName +" firstAppareance= "+ this.firstAppearence;
	}
	
	static boolean findByShortName(List<Character> list, Character character){
		for(int i = 0; i < list.size(); i++)
			if(character.getShortName().equalsIgnoreCase(list.get(i).getShortName()))
				return true;
		return false;
	}
	
	public static Character returnByShortName(List<Character> list, String shortName){
		for(int i = 0; i < list.size(); i++)
			if(list.get(i).getShortName().equalsIgnoreCase(shortName))
				return list.get(i);
		return null;
		
	}
	
	
	static boolean findByName(List<Character> list, String name){
		for(int i = 0; i < list.size(); i++)
			if(list.get(i).getName().equalsIgnoreCase(name))
				return true;
		return false;
	}
	
	static Character returnByName(List<Character> list, String name){
		for(int i = 0; i < list.size(); i++)
			if(list.get(i).getName().equalsIgnoreCase(name))
				return list.get(i);
		return null;
	}

	public boolean isMainCharacter() {
		if(name.equalsIgnoreCase("Philip J. Fry") ||
		   name.equalsIgnoreCase("Dr. John A. Zoidberg") ||
		   name.equalsIgnoreCase("Bender Bending Rodriguez") ||
		   name.equalsIgnoreCase("Prof Hubert J. Farnsworth") ||
		   name.equalsIgnoreCase("Turanga Leela") ||
		   name.equalsIgnoreCase("Hermes Conrad"))
			return true;
			
		return false;
	}
	
	public static class CharacterComparator implements Comparator{

		@Override
		public int compare(Object o1, Object o2) {
			if(!o1.getClass().equals(Character.class) || !o2.getClass().equals(Character.class))
				return 0;
			else {
				Character c1 = (Character)o1;
				Character c2 = (Character)o2;
				return String.CASE_INSENSITIVE_ORDER.compare(c1.getShortName(), c2.getShortName());
			}
				
		}
		
	}
	
}
